<?php 


class TabMenuSection {
		
	private $CONF;
		
	private $noOfPixelLastTabSmaller;
	private $lastTabWithDefaultWidth;
	private $tabWidth      = 0;	
	private $noOfEmptyTabs = 0;
	
	public $TMSVersion = '1.0';
	public $language;		
	public $tab;
	
    	  		
	function TabMenuSection($CONF)
	{
		$this->CONF = $CONF;
		unset($CONF);				    
        
		$this->language = $this->CONF['defaultLanguage'];    

        if(empty($this->CONF['tabPaddingTop']))
        {
            $this->CONF['tabPaddingTop'] = 0;
        }
	}

	public function getCSSPath($printCSSPath = false)
	{	    
	    $path = $this->CONF['pathTMS'].'TabMenuSectionStyles.css';
	    
	    if($printCSSPath)
	    {
	        echo $path;
	    }
	    return $path;
	}
	
	private function getPageURL()
	{
	    return $_SERVER['REQUEST_URI'];
	}
	
	private function getBorderDiv($height, $float)
	{
	    return '<div style="width: 0px; height: '.$height.'px; float: '.$float.'; border-right-style: solid; border-right-width: 1px; border-right-color: #C1C2C6;"></div>';
	}
	
	private function getContentForTab($tabNo)
	{
	    if(!empty($this->tab[$tabNo]['content']))
	    {	        
	        return $this->tab[$tabNo]['content'];
	    }
	    return '';
	}

    private function getTabName($no)
    {        
        if(!empty($this->tab[$no]['name']))
        {
            return $this->tab[$no]['name'];
        }     
        else if(!empty($this->CONF['tabs'][$this->language][$no]))
        {
            return $this->CONF['tabs'][$this->language][$no];
        }
        else if(!empty($this->CONF['tabs'][$this->CONF['defaultLanguage']][$no]))
        {
           return $this->CONF['tabs'][$this->CONF['defaultLanguage']][$no]; 
        }
        return '';
    }
        
    private function getTabWidth($no)
    {                        
        if(!empty($this->CONF['tabWidthUserDefined'][$no]))
        {
            return $this->CONF['tabWidthUserDefined'][$no];
        }
        else if($no == $this->lastTabWithDefaultWidth)
        {    	
    	    return $this->tabWidth - $this->noOfPixelLastTabSmaller;	   	
        }
                        
        return $this->tabWidth;
    }
	
    private function calculateTabs()
    {       
    	if(empty($this->CONF['noOfTabs']))
        {
            $this->CONF['noOfTabs'] = 1;
        }
        
        $this->lastTabWithDefaultWidth  = $this->CONF['noOfTabs'];        
        $totalWidthForTabs              = $this->CONF['totalWidth'] - ($this->CONF['noOfTabs'] + 1);
        $noOfTabsWithDefaultWidth       = $this->CONF['noOfTabs'];
                      
        if(isset($this->CONF['tabWidthUserDefined']))
        {            
             $noOfTabsWithDefaultWidth -= count($this->CONF['tabWidthUserDefined']);
                
            for($c = 1 ;$c <= $this->CONF['noOfTabs']; $c++)
            {               
                if(!empty($this->CONF['tabWidthUserDefined'][$c]))
                {                        
                    $totalWidthForTabs -= $this->CONF['tabWidthUserDefined'][$c];           
                }
                else
                {
                    $this->lastTabWithDefaultWidth = $c;
                }
            }
        }    
 
        if($totalWidthForTabs > 0 && $noOfTabsWithDefaultWidth <= 0)
        {
            $noOfTabsWithDefaultWidth = 1;
            $this->noOfEmptyTabs = 1;        
        }       
        
        if($totalWidthForTabs > 0 && $noOfTabsWithDefaultWidth > 0)
        {               
            while($totalWidthForTabs % $noOfTabsWithDefaultWidth)
            {
            	$this->noOfPixelLastTabSmaller++;
            	$totalWidthForTabs++;
            }
    
            $this->tabWidth = $totalWidthForTabs / $noOfTabsWithDefaultWidth;
        }
    }
    
    private function convertTabNamesToHTMLEntities()
    {
        //Convert special chars to HTML-Entities
        if(isset($this->CONF['tabs']))
        {
            foreach($this->CONF['tabs'] as $langKey => $row)
            {
                foreach($row as $noKey => $value)
                {
                   $this->CONF['tabs'][$langKey][$noKey] = htmlentities($value);       
                }
            }
        } 
    }
    
    public function generateTabMenuSection($printMenu = false, $convertTabNamesToHTMLEntities = true)    
    {                                                                      
        $this->calculateTabs();
        if($convertTabNamesToHTMLEntities)
        {
            $this->convertTabNamesToHTMLEntities();
        }
                         		      
        $noOfTabsCalculated = $this->CONF['noOfTabs'] + $this->noOfEmptyTabs;        
    	$sum                = 0;
    	
    	$tabContent         = '';
    	$tabName            = '';
    	
    	$borderDiv          = $this->getBorderDiv($this->CONF['tabHeight']+2, 'left');
    	$menu               = '<div class="pronuxTabGeneralStyle" style="width: '.$this->CONF['totalWidth'].'px;">';    
        $menu              .= $borderDiv;    
    	    	
    	for($x = 1; $x <= $noOfTabsCalculated; $x++)
    	{	    	    
    	    $tabName      = $this->getTabName($x);	       	    
    	    $normalDivTab = '<div id="pronuxTab'.$x.'" class="pronuxTabNormal" style="background-color: #'.$this->CONF['tabColor'].'; height: '.$this->CONF['tabHeight'].'px; width: '.$this->getTabWidth($x).'px;">';
    		
    	    if(!empty($tabName) || $x < $noOfTabsCalculated)
    		{
    		    $menu .= $normalDivTab;
    		    $menu .= ' <a class="pronuxLink" hidefocus="true"; style="padding-top: '.$this->CONF['tabPaddingTop'].'px; height: '.$this->CONF['tabHeight'].'px; line-height: '.$this->CONF['tabHeight'].'px; width: '.$this->getTabWidth($x).'px;" href="'.$this->getPageURL().'#!tab'.$x.'" onmouseover="this.style.textDecoration = \'none\'" onclick="setTab('.$x.')">'.$tabName.'</a>';    		    		     
    		    $menu .= '</div>';	
    		    $menu .= $borderDiv;  
    		}
    		else if($this->getTabWidth($x) <= 0)
    		{
    		    continue;
    		}
    		else
    		{
    		    if($this->CONF['useATabAsPlaceholder'])
    		    { 	    
    		        $menu .= str_replace('id="pronuxTab'.$x.'" ', '', $normalDivTab);
    		        $menu .= '</div>';	    
    		        $menu .= $borderDiv;
    		    }
    		    else
    		    {
    	            $menu .= '<div class="pronuxTabEmpty" style="height: '.($this->CONF['tabHeight'] + 1).'px; width: '.($this->getTabWidth($x)+1).'px;"></div>';    	                	            
    		    }
    		    continue;	    	            	                	        	        	        		     		    	
    		}    		   		    	   		    		  		    	    		

		    $tabContent .= '<div id="tabContent'.$x.'" class="pronuxOuterContentBox" style="background-color: #'.$this->CONF['contentBackgroundColor'].'; width: '.$this->CONF['totalWidth'].'px;">';
            $tabContent .= '<div class="pronuxContentBox" style="min-height: '.$this->CONF['defaultContentSectionHeight'].'px; height: '.$this->CONF['defaultContentSectionHeight'].'px;">';
            $tabContent .= $this->getContentForTab($x);
            $tabContent .= '</div></div>';   
    	}	
    	    	
     	$menu .= '<div style="float: left;">';   	        	     	
        $menu .= $tabContent; 	     	
        $menu .= '</div>'; 		
        $menu .= '</div><script language="javascript">';    
    
        $startJScript = "if(document.getElementById('pronuxTab' + self.document.location.hash.substr(5,1)) != null)
                        {
                        	setTab(self.document.location.hash.substr(5,1));
                        }
                        else
                        {
        	               	setTab(1);
    	                }";
    
        if(!empty($_GET['_escaped_fragment_']))
        {
            
            if(strlen($_GET['_escaped_fragment_']) == 4)
            {
               $startJScript = 'setTab('.substr($_GET['_escaped_fragment_'],-1).');'; 
            }
            else if(strlen($_GET['_escaped_fragment_']) == 5)
            {
                $startJScript = 'setTab('.substr($_GET['_escaped_fragment_'],-2).');'; 
            }
        }
        
        $menu .= $startJScript;
        
        
        $menu .= '
        function setTab(tabNo)
        {
        	for (z=1;z<100;z++)
        	{
        		tab = document.getElementById(\'pronuxTab\' + z);
        		if(tab == null)
        		{
        			break;
        		}
        		tab.style.borderBottomColor = \'#C1C2C6\';		
        		tab.style.backgroundColor = \'#'.$this->CONF['tabColor'].'\';
        
        		document.getElementById(\'tabContent\' + z).style.display = \'none\';				
        	}
        	
        	document.getElementById(\'pronuxTab\' + tabNo).style.backgroundColor = \'#'.$this->CONF['contentBackgroundColor'].'\';
          	document.getElementById(\'pronuxTab\' + tabNo).style.borderBottomColor = \'#'.$this->CONF['contentBackgroundColor'].'\';
        	document.getElementById(\'tabContent\' + tabNo).style.display = \'block\';	        	
        }
        </script>'; 
        
        $menu = str_replace("\n", '', $menu);
        $menu = str_replace("\r", '', $menu);
		$menu = str_replace("\t", '', $menu);       

        if($printMenu)
        {
            echo $menu;
        }
        
        return $menu;
    }
    
    public function setLanguage($languageCode = '', $languageName = '')
    {
        $languageCode = substr($languageCode, 0, 2);
        $languageName = substr($languageName, 0, 30);
        
        if(!empty($languageCode))
        {
            $this->language = $languageCode;
        }
        else if(!empty($languageName) && !empty($this->CONF['languages'][$languageName]))
        {            
            $this->language = $this->CONF['languages'][$languageName];
        }        
    }
    
    public function setNoOfTabs($noOfTabs)
    {
        $this->CONF['noOfTabs'] = (int)$noOfTabs;        
    }    
}


?>